package com.foxwho.springbootunittesting.controller;

import com.foxwho.springbootunittesting.model.Book;
import com.foxwho.springbootunittesting.service.HiService;
import com.foxwho.springbootunittesting.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    HiService hiService;

    public BookController(HiService hiService) {
        this.hiService = hiService;
    }

    @PostMapping("/create")
    public String create(@RequestBody Book book) {
        log.info("book={}", book);
        String data = JsonUtil.toString(book);
        log.info("data={}", data);
        return data;
    }

    @PostMapping("/hi")
    public String hi(@RequestBody Book book) {
        log.info("book={}", book);
        String data = JsonUtil.toString(book);
        return hiService.hi(data);
    }

    @GetMapping("/info")
    public String info() {
        Book book = new Book();
        book.setName("JAVA 高级编程");
        book.setDate(LocalDate.now());

        log.info("book={}", book);
        String data = JsonUtil.toString(book);
        log.info("data={}", data);
        return data;
    }

    @GetMapping("/query")
    public String query(@RequestParam("name") String name) {
        log.info("name={}", name);
        return name;
    }

    @PostMapping("/search")
    public String search(@RequestParam("name") String name, @RequestParam("date") String date) {
        log.info("name={}", name);
        log.info("date={}", date);
        return name;
    }
}
